from ui.usermanager import *
from service import *
from addUser import *
from PyQt5.QtWidgets import QHeaderView
from PyQt5.QtGui import  QStandardItemModel,QStandardItem

class UserManagerMainWindow(Ui_UserManagerMainWindow, QtWidgets.QMainWindow):
    def __init__(self, ) -> None:
        super().__init__()
        self.setupUi(self)
        self.queryUser()
        self.pushButton.clicked.connect(self.addUser)
        self.pushButton_2.clicked.connect(self.queryUser)

    def addUser(self):
        self.newWindow=AddUserMainWindow()
        self.newWindow.show()


    def queryUser(self):
        username = self.lineEdit_2.text()
        us = UserService()
        if username:
            user_list = us.getUserList(username)
        else:
            user_list = us.getUserList()

        self.model=QStandardItemModel(4,4)#存储任意结构数据
        self.model.setHorizontalHeaderLabels(['用户名','密码','邮箱','角色'])
        row = 0
        for u in user_list:
            column = 0
            self.model.setItem(row,column,QStandardItem("%s"%(u.username)))
            self.model.setItem(row,column+1,QStandardItem("%s"%(u.password)))
            self.model.setItem(row,column+2,QStandardItem("%s"%(u.email)))
            self.model.setItem(row,column+3,QStandardItem("%s"%(u.role)))
            row += 1
        self.tableView.setModel(self.model)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)

'''
    def queryAllUser(self):
        us = UserService()
        user_list = us.getAllUser()
        print(user_list)
        self.model=QStandardItemModel(4,4)#存储任意结构数据
        self.model.setHorizontalHeaderLabels(['用户名','密码','邮箱','角色'])
        row = 0
        for u in user_list:
            column = 0
            self.model.setItem(row,column,QStandardItem("%s"%(u.username)))
            self.model.setItem(row,column+1,QStandardItem("%s"%(u.password)))
            self.model.setItem(row,column+2,QStandardItem("%s"%(u.email)))
            self.model.setItem(row,column+3,QStandardItem("%s"%(u.role)))
            row += 1

        # for row in range(4):
        #     for column in range(4):
        #         i=QStandardItem("  row %s,column %s"%(row,column))
        #         self.model.setItem(row,column,i)
        self.tableView.setModel(self.model)
        self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
'''